package com.inspector.app.ui.task

import androidx.lifecycle.LifecycleOwner
import com.example.commen.net.easyHttp.EasyHttpManager
import com.hjq.toast.Toaster
import com.inspector.app.di.util.fromJson
import com.inspector.app.ui.vo.Params
import com.inspector.app.ui.vo.TaskData
import com.inspector.app.ui.vo.TaskItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.transform
import kotlinx.coroutines.withContext

class TaskRepository(
    private val lifecycleOwner: LifecycleOwner
) {

    /**
     * 获取协同任务列表数据
     * @param systemTask 是否是系统任务列表 true 系统任务 false 我的任务
     * @param pageNumber 页码
     * @param pageSize 每页大小
     * @param taskTitle 任务标题
     * @param status 任务状态
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    fun getTaskItems(
        systemTaskItem: Params
    ): Flow<TaskData> {
        return flow { emit(systemTaskItem) }.transform {
            emit(
                if (it.systemTaskPos == true) getSystemTask() else getMyTask(systemTaskItem)
            )
        }
    }

    private suspend fun getSystemTask(): TaskData {
       val items =  withContext(Dispatchers.IO) {
            try {
                val httpData =
                    EasyHttpManager.get().getHttpData(lifecycleOwner, "/api/v2/cooperationTask")
                        ?: return@withContext mutableListOf()
                if (httpData.code != 200) {
                    withContext(Dispatchers.Main) {
                        Toaster.show("${httpData.message}")
                    }
                    return@withContext mutableListOf()
                }
                fromJson<MutableList<TaskItem>>(httpData.data) ?: mutableListOf()
            } catch (e: Exception) {
                e.printStackTrace()
                mutableListOf()
            }
        }

        return TaskData().apply {
            list = items
        }
    }


    private suspend fun getMyTask(
        params: Params
    ): TaskData {
        return withContext(Dispatchers.IO) {
            try {
                val url = "/api/v1/cooperationTask/queryByPage" +
                        "?pageNumber=${params.pageNumber}" +
                        "&pageSize=${params.pageSize}" +
                        "&taskTitle=${params.taskTitle}" +
                        "&status=${params.status}" +
                        "&startTime=${params.startTime}" +
                        "&endTime=${params.endTime}"
                val httpData = EasyHttpManager.get().getHttpData(lifecycleOwner, url)
                    ?: return@withContext TaskData()
                if (httpData.code != 200) {
                    withContext(Dispatchers.Main) {
                        Toaster.show("${httpData.message}")
                    }
                    return@withContext TaskData()
                }
                fromJson<TaskData>(httpData.data) ?: TaskData()
            } catch (e: Exception) {
                e.printStackTrace()
                TaskData()
            }
        }
    }


}